package 多线程;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * FixedThreadPool
 *
 * @作者 SystemMgtApe
 * @创建时间 2022年03月30日 10:43:45
 */
public class FixedThreadPool {

    static class TestCallable<T> implements Callable<String> {
        /**  */
        @Override
        public String call() throws Exception {
            System.out.println("执行");
            Thread.sleep(2000);
            return "返回结果";
        }

    }

    public static void main(String[] args) throws Exception {
        System.out.println("—————————— 测试开始 ——————————");
        // 创建一个可重用固定线程数的线程池
        ExecutorService pool = Executors.newFixedThreadPool(3);
        // 创建线程
        Future<String> aa = pool.submit(new TestCallable<>());
        Future<String> bb = pool.submit(new TestCallable<>());
        Future<String> cc = pool.submit(new TestCallable<>());
        Future<String> dd = pool.submit(new TestCallable<>());

        String a = aa.get();
        String b = bb.get();
        String c = cc.get();
        String d = dd.get();
        System.out.println("a: " + a);
        System.out.println("b: " + b);
        System.out.println("c: " + c);
        System.out.println("d: " + d);
        System.out.println("—————————— 测试结束 ——————————");
        pool.shutdown();
    }
}
